package com.kmdd.xtds.util

import android.content.Context
import android.graphics.Color
import com.kmdd.xtds.R
import com.kmdd.xtds.room.BloodPressure
import com.github.mikephil.charting.charts.BarChart
import com.github.mikephil.charting.components.XAxis
import com.github.mikephil.charting.components.YAxis
import com.github.mikephil.charting.data.BarData
import com.github.mikephil.charting.data.BarDataSet
import com.github.mikephil.charting.data.BarEntry
import com.github.mikephil.charting.formatter.ValueFormatter
import com.guangli.data.util.HeartCustomBarChartRender
import kotlin.math.roundToInt
import kotlin.reflect.typeOf

fun initHeartChartView(
    barChart: BarChart
) {
    val barChartRender =
        HeartCustomBarChartRender(barChart, barChart.animator, barChart.viewPortHandler)
    barChartRender.setRadius(20f)
    barChart.renderer = barChartRender
    barChart.xAxis.textSize = 10f
    barChart.xAxis.textColor = Color.parseColor("#9C9C9C")
    barChart.xAxis.position = XAxis.XAxisPosition.BOTTOM
//    barChart.xAxis.setAvoidFirstLastClipping(true)
    barChart.xAxis.setDrawAxisLine(true) //横线
    barChart.xAxis.axisLineColor = Color.parseColor("#E2E2E2")
    barChart.xAxis.setDrawGridLines(false) //垂线
    barChart.xAxis.setDrawGridLinesBehindData(false)
    barChart.xAxis.setDrawLabels(true)
    barChart.xAxis.granularity = 1f
    //如果设置为 true，则在每个条形后面绘制一个灰色区域，指示最大值。启用他将使性能降低约 40%。
    barChart.setDrawBarShadow(false)
    barChart.axisRight.isEnabled = false
    barChart.axisLeft.isEnabled = true
    //去掉左侧Y轴刻度
    barChart.axisLeft.setDrawLabels(true)
    //去掉左侧Y轴
    barChart.axisLeft.setDrawAxisLine(false)
    //去掉中间横线
    barChart.axisLeft.setDrawGridLines(true)
    barChart.axisRight.setDrawAxisLine(false);
//    barChart.axisRight.setDrawZeroLine(true)
    barChart.axisLeft.textColor = Color.parseColor("#9C9C9C")
    barChart.axisLeft.axisLineColor = Color.parseColor("#E2E2E2")
    barChart.axisLeft.gridColor = Color.parseColor("#E2E2E2")
    barChart.axisLeft.textSize = 10f
    barChart.xAxis.yOffset = 8f

    barChart.setScaleEnabled(false)//设置是否可以缩放
    barChart.setTouchEnabled(true)//设置是否可以触摸
    barChart.isDragEnabled = true

    barChart.setDrawValueAboveBar(false)//这里设置为true每一个直方图的值就会显示在直方图的顶部
    barChart.description.isEnabled = false//设置描述不显示
    barChart.isHighlightFullBarEnabled = false
//    barChart.axisRight.spaceTop = 20f
    barChart.isHighlightPerTapEnabled = false
    barChart.setFitBars(true)
    barChart.setPinchZoom(false)
//    barChart.minOffset = 30f
//    barChart.extraTopOffset = 50f
    barChart.extraBottomOffset = 25f
    barChart.setNoDataText("")
    barChart.legend.isEnabled = false
}

fun setHeartChartData(
    barChart: BarChart,
    bean: List<BloodPressure>,
    xMaxCount: Int
) {
    val barEntryList: MutableList<HeartBarEntry> = ArrayList()

    val size = bean.size.toFloat()
    val colorList11 = mutableListOf<Int>()
    bean.forEachIndexed { index, bloodPressure ->
        barEntryList.add(
            HeartBarEntry(
                index.toFloat(),
                110f,
                bloodPressure.shrink.toInt(),
                bloodPressure.diastolicBloodPressure.toInt(),
                1
            )
        )
        colorList11.add(Color.parseColor("#1B84FF"))
    }

    val barDataSet = BarDataSet(barEntryList as List<BarEntry>?, "")
    barDataSet.setDrawValues(false)
    barDataSet.axisDependency = YAxis.AxisDependency.LEFT
    barDataSet.barShadowColor = Color.parseColor("#F1F1F1")
    val barData = BarData(
        barDataSet
    )
    barDataSet.colors = colorList11

    var barWidth = 0.02f * size
    if (barWidth > 0.7) {
        barWidth = 0.7f
    }
//    else if (barWidth < 0.2f) {
//        barWidth = 0.2f
//    }


//    val list = bean?.axisX?.map {
//        it.sort!!.toFloat()
//    } ?: arrayListOf()

    barData.barWidth = barWidth
    barChart.data = barData //先设置数据
////    barChart.xAxis.mEntries = list.toFloatArray()//list.toTypedArray()
    barChart.xAxis.axisMaximum = bean.size.toFloat() - barWidth / 2//+3f
    barChart.xAxis.axisMinimum = 0f- barWidth / 2
////    barChart.xAxis.spaceMin = 1f
////    barChart.xAxis.spaceMin = 1f
//
//
    barChart.xAxis.setLabelCount(
        if (size >= xMaxCount) {
            xMaxCount
//            size.toInt()
        } else {
            size.toInt()
        }, false
    )
    barChart.setVisibleXRange(
        1f,
        if (size >= xMaxCount) {
            xMaxCount.toFloat()
//            size
        } else {
            size
        }
    )
    barChart.xAxis.valueFormatter =
        object : ValueFormatter() {
            override fun getFormattedValue(value: Float): String {
//                LogUtils.e("${value}   ${value.roundToInt()}")
                return getRideIndexResult(bean, value.roundToInt())//"${}"
            }
        }

    //然后再设置分组柱状图
    barChart.axisLeft.axisMinimum = 0f

    barChart.axisLeft.axisMaximum = 200f
//    barChart.setVisibleYRangeMaximum(5f, YAxis.AxisDependency.RIGHT)
    barChart.axisLeft.setLabelCount(5, true)

//    barChart.axisRight.valueFormatter = object : ValueFormatter() {
//        override fun getFormattedValue(value: Float): String {
////            LogUtils.e("${value}-----${value.roundToInt()}")
//            return getRideIndexResult(
//                yList,
//                value.roundToInt().toFloat()
//            )
//        }
//    }
//    val mMatrix = Matrix()
//    mMatrix.postScale((bean?.axisX?.size?.toFloat() ?: 0f) / 16f, 1f)//
//    barChart.viewPortHandler.refresh(mMatrix, barChart, true)

    barChart.animateXY(1000, 1000)
    barChart.data.notifyDataChanged()
    barChart.notifyDataSetChanged()
    barChart.invalidate()
}

fun getRideIndexResult(list: List<BloodPressure>, value: Int): String {
    if (value < list.size) {
        return list[value].time
    }
    return ""
}



